/*
 * Copyright (c) 2023 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <cstring>
#include <iostream>
#include <vector>
#include <string>
#include "gtest/gtest.h"

#include "net_http.h"
#include "netstack_hash_map.h"
#include "netstack_log.h"
#include "net_http_inner_types.h"
#include "http_client_request.h"
#include "http_client.h"
#include "http_client_constant.h"
#include "net_http_type.h"

using namespace OHOS::NetStack::HttpClient;

class NetHttpTest : public testing::Test {
public:
    static void SetUpTestCase() {}

    static void TearDownTestCase() {}

    virtual void SetUp() {}

    virtual void TearDown() {}
};

namespace {
using namespace std;
using namespace testing::ext;

static void myCallbackFunction()
{
    NETSTACK_LOGI("myCallbackFunction function called!");
}

static void testResponseCallback(struct Http_Response *response, uint32_t errCode)
{
    NETSTACK_LOGI("testResponseCallback function called!");
}

static void TestDataReceiveCallback(const char *data, size_t length)
{
    NETSTACK_LOGI("testDataReceiveCallback function called!");
    return;
}

static void testHeaderReceiveCallback(Http_Headers *headers)
{
    NETSTACK_LOGI("testHeaderReceiveCallback function called!");
}

HWTEST_F(NetHttpTest, CreateHeaders001, TestSize.Level1)
{
    Http_Headers *header = OH_Http_CreateHeaders();
    EXPECT_TRUE(header != nullptr);
    OH_Http_DestroyHeaders(&header);
}

HWTEST_F(NetHttpTest, DestroyHeaders001, TestSize.Level1)
{
    Http_Headers *header = OH_Http_CreateHeaders();
    EXPECT_TRUE(header != nullptr);
    EXPECT_EQ(OH_Http_SetHeaderValue(header, "key", "value"), OH_HTTP_RESULT_OK);
    OH_Http_DestroyHeaders(&header);
}

HWTEST_F(NetHttpTest, DestroyHeaders002, TestSize.Level1)
{
    Http_Headers *header = nullptr;
    OH_Http_DestroyHeaders(&header);

    OH_Http_DestroyHeaders(nullptr);
}

HWTEST_F(NetHttpTest, ToLowerCase001, TestSize.Level1)
{
    Http_Headers *header = OH_Http_CreateHeaders();
    EXPECT_TRUE(header != nullptr);
    
    OH_Http_SetHeaderValue(header, "TEST", "value");
    // 验证是否转为小写可通过遍历 headers 或后续获取验证，此处仅测试接口调用
    OH_Http_DestroyHeaders(&header);
}

HWTEST_F(NetHttpTest, SetHeaderValue001, TestSize.Level1)
{    
    uint32_t ret = OH_Http_SetHeaderValue(nullptr, "key", "test");
    EXPECT_EQ(ret, OH_HTTP_PARAMETER_ERROR);

    Http_Headers *header = OH_Http_CreateHeaders();
    EXPECT_TRUE(header != nullptr);
    ret = OH_Http_SetHeaderValue(header, nullptr, "test");
    EXPECT_EQ(ret, OH_HTTP_PARAMETER_ERROR);

    ret = OH_Http_SetHeaderValue(header, "key2", nullptr);
    EXPECT_EQ(ret, OH_HTTP_PARAMETER_ERROR);
    OH_Http_DestroyHeaders(&header);
}

HWTEST_F(NetHttpTest, SetHeaderValue002, TestSize.Level1)
{    
    Http_Headers *header = OH_Http_CreateHeaders();
    EXPECT_TRUE(header != nullptr);
    OH_Http_SetHeaderValue(header, "key1", "test1");

    uint32_t ret = OH_Http_SetHeaderValue(header, "key2", "test2");
    EXPECT_EQ(ret, OH_HTTP_RESULT_OK);
    OH_Http_DestroyHeaders(&header);
}

HWTEST_F(NetHttpTest, SetHeaderValue003, TestSize.Level1)
{    
    Http_Headers *header = OH_Http_CreateHeaders();
    EXPECT_TRUE(header != nullptr);
    OH_Http_SetHeaderValue(header, "key1", "test1");

    uint32_t ret = OH_Http_SetHeaderValue(header, "key1", "test1");
    EXPECT_EQ(ret, OH_HTTP_RESULT_OK);
    OH_Http_DestroyHeaders(&header);
}

HWTEST_F(NetHttpTest, SetHeaderValue004, TestSize.Level1)
{    
    Http_Headers *header = OH_Http_CreateHeaders();
    EXPECT_TRUE(header != nullptr);
    OH_Http_SetHeaderValue(header, "key1", "test1");

    uint32_t ret = OH_Http_SetHeaderValue(header, "key1", "test2");
    EXPECT_EQ(ret, OH_HTTP_RESULT_OK);
    OH_Http_DestroyHeaders(&header);
}

HWTEST_F(NetHttpTest, SetHeaderValue005, TestSize.Level1)
{    
    Http_Headers *header = OH_Http_CreateHeaders();
    EXPECT_TRUE(header != nullptr);
    OH_Http_SetHeaderValue(header, "key1", "test1");
    
    // 模拟容量溢出情况
    header->fields->capacity = MAX_MAP_CAPACITY + 1;
    uint32_t ret = OH_Http_SetHeaderValue(header, "key1", "test2");
    EXPECT_EQ(ret, OH_HTTP_PARAMETER_ERROR);
    OH_Http_DestroyHeaders(&header);
}

HWTEST_F(NetHttpTest, GetHeaderValue001, TestSize.Level1)
{       
    Http_Headers *header = OH_Http_CreateHeaders();
    EXPECT_TRUE(header != nullptr);
    header->fields = nullptr;
    Http_HeaderValue *value = OH_Http_GetHeaderValue(header, "key");
    EXPECT_EQ(value, nullptr);
    OH_Http_DestroyHeaders(&header);
}
}